package com.apotik.legenda.dialog;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import java.awt.Event;
import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.table.TableModel;

import java.awt.Color;

import javax.swing.SwingConstants;
import javax.swing.JButton;

import com.apotik.legenda.custom.TableModelCustom;
import com.apotik.legenda.dao.DoctorDao;
import com.apotik.legenda.model.Doctor;
import com.apotik.legenda.model.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.util.Vector;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class AddEditDokterDialog extends JDialog implements ActionListener {
	private static final long serialVersionUID = 1L;

	private JPanel panel;
	
	private JLabel lblKodeDokter;
	private JLabel lblNamaDokter;
	private JLabel lblAlamat;
	private JLabel lblHp;
	private JLabel lblEmail;
	
	private JTextField kodeDokterField;
	private JTextField namaDokterField;
	private JTextArea alamatField;
	private JTextField hpField;
	private JTextField emailField;
	
	private JButton btnSave;
	private JButton btnCancel;
	
	private TableModelCustom tableModel;
	private User user;
	
	private Integer rowIndex = -1;
	private Integer doctorId = -1;
	
	
	public AddEditDokterDialog(String title, TableModelCustom tableModel, User user) {
		setTitle(title);
		setSize(387, 251);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ESCAPE){
					AddEditDokterDialog.this.setVisible(false);
				} else if(e.getKeyCode()==KeyEvent.VK_ENTER){
					
				}
			}
		});
		
		

		this.tableModel = tableModel;
		this.user = user;
		
		panel = new JPanel();
		panel.setLayout(null);
		getContentPane().add(panel);
		
		lblKodeDokter = new JLabel("Kode Dokter");
		lblKodeDokter.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblKodeDokter.setBounds(15, 15, 90, 16);
		panel.add(lblKodeDokter);
		
		kodeDokterField = new JTextField();
		kodeDokterField.setBounds(110, 13, 242, 20);
		panel.add(kodeDokterField);
		kodeDokterField.setColumns(10);
		
		lblNamaDokter = new JLabel("Nama Dokter");
		lblNamaDokter.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblNamaDokter.setBounds(15, 41, 90, 16);
		panel.add(lblNamaDokter);
		
		namaDokterField = new JTextField();
		namaDokterField.setBounds(110, 38, 242, 20);
		panel.add(namaDokterField);
		//namaBarangField.setColumns(10);
		
		lblAlamat = new JLabel("Alamat");
		lblAlamat.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblAlamat.setBounds(15, 64, 77, 16);
		panel.add(lblAlamat);
		
		alamatField = new JTextArea();
		alamatField.setBounds(110, 63, 242, 45);
		panel.add(alamatField);
		alamatField.setColumns(10);
		
		lblHp = new JLabel("No. Handphone");
		lblHp.setHorizontalAlignment(SwingConstants.LEFT);
		lblHp.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblHp.setBounds(15, 116, 90, 16);
		panel.add(lblHp);
		
		hpField = new JTextField();
		hpField.setBounds(110, 114, 242, 20);
		panel.add(hpField);
		
		lblEmail = new JLabel("Email");
		lblEmail.setHorizontalAlignment(SwingConstants.LEFT);
		lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblEmail.setBounds(15, 142, 77, 16);
		panel.add(lblEmail);
		
		emailField = new JTextField();
		emailField.setBounds(110, 140, 242, 20);
		panel.add(emailField);
		
		JPanel boxPanel = new JPanel();
		boxPanel.setBorder(new LineBorder(new Color(169, 169, 169)));
		boxPanel.setBounds(5, 4, 360, 170);
		panel.add(boxPanel);
		
		btnSave = new JButton("Simpan");
		btnSave.setBounds(203, 180, 80, 25);
		btnSave.addActionListener(this);
		panel.add(btnSave);
		
		btnCancel = new JButton("Batal");
		btnCancel.setBounds(285, 180, 80, 25);
		btnCancel.addActionListener(this);
		panel.add(btnCancel);
		
		
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void actionPerformed(ActionEvent e) {
		kodeDokterField.setFocusable(true);
		Object obj = e.getSource();
		
		if(obj==btnSave){
			String kodeDokter 	= kodeDokterField.getText();
			String namaDokter 	= namaDokterField.getText();
			String address 		= alamatField.getText();
			String phone		= hpField.getText();
			String email		= emailField.getText();
			
			Doctor doctor = new Doctor();
			if(doctorId!=-1){
				doctor.setDoctorId(doctorId);
			}
			doctor.setDoctorCode(kodeDokter);
			doctor.setFullName(namaDokter);
			doctor.setCreatedDate(Calendar.getInstance().getTime());
			doctor.setDoctorStatus(true);
			doctor.setPhoneNumber(phone);
			doctor.setAddress(address);
			doctor.setEmail(email);
			doctor.setActive(true);
			doctor.setCreatedBy(user);
			
			DoctorDao dao = new DoctorDao();
			boolean isSuccess = dao.addNewOrUpdate(doctor);
			if(isSuccess){
				Vector v = new Vector();
				v.add(kodeDokter);
				v.add(namaDokter);
				v.add(phone);
				v.add(email);
				v.add("Aktif");
				v.add(doctor);
				if(doctorId==-1){
					tableModel.insertData(v);
				}else{
					tableModel.updateData(this.rowIndex, v);
				}
			}
			
			AddEditDokterDialog.this.setVisible(false);
			
		}else if(obj==btnCancel){
			AddEditDokterDialog.this.setVisible(false);
			AddEditDokterDialog.this.dispose();
		}
	}
	
	public void fillInTheFields(int rowIndex, Doctor doctor){
		if(doctor!=null){
			this.rowIndex = rowIndex;
			this.doctorId = doctor.getDoctorId();
			
			kodeDokterField.setText(doctor.getDoctorCode());
			namaDokterField.setText(doctor.getFullName());
			alamatField.setText(doctor.getAddress());
			hpField.setText(doctor.getPhoneNumber());
			emailField.setText(doctor.getEmail());
		}
	}
	
	public void btnSaveSetText(String text){
		btnSave.setText(text);
	}
	
}
